<?php


namespace app\controller\v1;


use app\model\Change;
use app\model\MoneyLog;
use app\services\SendCode;
use app\validate\UserValidate;
use think\cache\driver\Redis;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
use think\Request;

/**
 * 用户
 * Class User
 * @package app\controller\v1
 */
class User
{

    /**
     * User constructor.
     * @param \app\model\User $user
     */
    public function __construct(\app\model\User $user)
    {
        $this->model = $user;
    }


    /**
     * 注册
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function Register(Request $request)
    {
        return $this->model->Register($request->data);
    }


    /**
     * 登录
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function Login(Request $request)
    {
        return $this->model->Login($request->data);
    }


    /**
     * 小程序登录
     */
    public function WxLogin(Request $request)
    {
        return $this->model->WxLogin($request->data);
    }


    /**
     * 用户信息
     * @param Request $request
     * @return mixed
     */
    public function UserInfo(Request $request)
    {
        return $this->model->UserInfo($request);
    }


    /**
     * 完善个人资料
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function realName(Request $request)
    {
        return $this->model->realName($request->user_id, $request->only(['username', 'card_num']));
    }


    //验证支付密码
    public function verifyPayPass(Request $request){
        return $this->model->verifyPayPass($request);
    }

    /**
     * 积分日志
     * @param Request $request
     * @param MoneyLog $moneyLog
     * @return mixed
     */
    public function MoneyLog(Request $request, MoneyLog $moneyLog)
    {
        return $moneyLog->log($request);
    }

    /**
     * 积分日志
     * @param Request $request
     * @param Change $change
     * @return mixed
     */
    public function ChangeLog(Request $request, Change $change)
    {
        return $change->log($request);
    }


    /**
     * 好友列表
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function UserList(Request $request)
    {
        return $this->model->UserList($request->data);
    }

    /**
     * 修改个人资料
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function UserModify(Request $request)
    {
        return $this->model->UserModify($request);
    }


    /**
     * 发送短信
     * @param Request $request
     * @return mixed
     */
    public function SendCode(Request $request)
    {
        return $this->model->SendCode($request);
    }

    /**
     * 完成新手教程
     */
    public function newbieTutorial(Request $request)
    {
        return $this->model->newbieTutorial($request);
    }


    /**
     * 退出登录
     * @param Request $request
     * @return mixed
     */
    public function Logout(Request $request)
    {
        return $this->model->Logout($request->user_id);
    }


    /**
     * 邀请链接
     * @param Request $request
     * @return mixed
     */
    public function inviteUrl(Request $request)
    {
        return $this->model->inviteUrl($request->user_id);
    }


    /**
     * 任务状态
     * @param Request $request
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function taskProgress(Request $request)
    {
        $arr['is_newbie_tutorial'] = $request->user->is_newbie_tutorial;
        $arr['is_real_name'] = $request->user->is_real_name;
        $arr['is_video'] = $request->user->is_video;
        $arr['is_complete_information'] = 0;
        $arr['is_sign'] = 0;
        $complete = Db::name('money_log')->where(['user_id' => $request->user_id, 'type' => 4])->count();
        if ($complete > 0) {
            $arr['is_complete_information'] = 1;
        }
        //查看今日是否签到
        $where[] = ['user_id', '=', $request->user_id];
        $where[] = ['type', '=', 3];
        $is_sign = Db::name('money_log')->where($where)->whereTime('created_time', 'between', [(DateTime()['t_s']), (DateTime()['t_e'])])->count();
        if ($is_sign > 0) {
            $arr['is_sign'] = 1;
        }
        $arr ['novice_task_bonus_points'] = SystemConfig('novice_task_bonus_points');
        $arr ['novice_beginner_guide'] = SystemConfig('novice_beginner_guide');
        $arr ['novice_real_name'] = SystemConfig('novice_real_name');
        $arr ['novice_task_video_num'] = SystemConfig('novice_task_video_num');
        $arr ['novice_task_video'] = SystemConfig('novice_task_video');
        //视频次数
        $redis = Cache::store('redis');
        $key = 'video_task_' . $request->user_id . '_' . date('Y-m-d', time());
        $today_count = $redis->get($key);
        $arr['video_num'] = $today_count ?: 0;
        return app('json')->json_success('成功', $arr);
    }
}